﻿@using Smartstore.Admin.Models.Common

@model DeliveryTimeModel

<datagrid id="delivery-time-grid" allow-resize="true" allow-row-selection="true" allow-edit="true" allow-column-reordering="true">
    <datasource read="@Url.Action("DeliveryTimeList", "DeliveryTime")"
                delete="@Url.Action("DeliveryTimeDelete", "DeliveryTime")" />
    <sorting enabled="true">
        <sort by="DisplayOrder" />
    </sorting>
    <paging position="Bottom" show-size-chooser="true" />
    <toolbar>
        <toolbar-group>
            <a href="#" class="add-delivery-time btn btn-primary btn-flat">
                <i class="fa fa-plus"></i>
                <span>@T("Admin.Common.AddNew")...</span>
            </a>
        </toolbar-group>
        <zone name="datagrid_toolbar_alpha"></zone>
        <toolbar-group class="omega"></toolbar-group>
        <zone name="datagrid_toolbar_omega"></zone>
        <toolbar-group>
            <button datagrid-action="DataGridToolAction.DeleteSelectedRows" type="button" class="btn btn-danger no-anims btn-flat">
                <i class="far fa-trash-alt"></i>
                <span>@T("Admin.Common.Delete.Selected")</span>
            </button>
        </toolbar-group>
    </toolbar>
    <columns>
        <column for="Id" halign="center" visible="false" type="string" />
        <column for="Name" width="2fr" hideable="false">
            <display-template>
                <div class="d-block">
                    <a href="#" class="grid-edit-delivery-time text-truncate" :data-key="item.row.Id">
                        {{ item.value }}
                    </a>
                    <div v-if="item.row.DeliveryInfo" class="muted" v-html="item.row.DeliveryInfo"></div>
                </div>
            </display-template>
        </column>
        <column for="ColorHexValue" sortable="false">
            <display-template>
                <span class='color-container'>
                    <span class='color' :style='{background: item.value}'>&nbsp;</span>
                </span>
                <span>{{ item.value }}</span>
            </display-template>
        </column>
        <column for="IsDefault" halign="center" sortable="false" />
        <column for="MinDays" halign="center" />
        <column for="MaxDays" halign="center" />
        <column for="DisplayOrder" halign="center" />
    </columns>
    <row-commands>
        <a datarow-action="DataRowAction.Custom" class="grid-edit-delivery-time" :data-key="item.row.Id">@T("Common.Edit")</a>
        <a datarow-action="DataRowAction.Custom" class="grid-set-default" :data-key="item.row.Id" v-bind:class="{ disabled: item.row.IsDefault }">@T("Common.SetDefault")</a>
        <div class="dropdown-divider"></div>
        <a datarow-action="DataRowAction.Delete">@T("Common.Delete")</a>
    </row-commands>
</datagrid>

<script sm-target-zone="scripts" data-origin="delivery-time-grid">
    $(function () {
        $(document).on('click', '.add-delivery-time', function (e) {
            e.preventDefault();

            var href = "@Url.Content("~/Admin/DeliveryTime/CreateDeliveryTimePopup/")?btnId=btnRefresh&formId=delivery-times";
            openPopup(href);
            return false;
        });

        $(document).on('click', '.grid-edit-delivery-time', function (e) {
            e.preventDefault();

            var id = $(this).data("key");
            var href = "@Url.Content("~/Admin/DeliveryTime/EditDeliveryTimePopup/")" + id + "?btnId=btnRefresh&formId=delivery-times";
            openPopup(href);
            return false;
        });

        $(document).on('click', '.grid-set-default', function (e) {
            e.preventDefault();
            
            $.ajax({
				cache: false,
				type: 'POST',
				url: '@(Url.Action("SetDefault", "DeliveryTime"))',
				data: {
					"id": $(this).data("key")
				},
                success: function () {
                    window["delivery-time-grid"].$children[0].read();
                }
            });

            return false;
        });

        // Refresh grid after popup edit dialog was closed.
        $(document).on('click', '#btnRefresh', function () {
            window["delivery-time-grid"].$children[0].read();
            return false;
        });
    });
</script>
